/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.jena.fuseki.servlets ;

import static java.lang.String.format ;

import java.util.List ;

import org.apache.jena.atlas.lib.InternalErrorException ;
import org.apache.jena.atlas.lib.Pair;
import org.apache.jena.fuseki.server.DataService;
import org.apache.jena.fuseki.server.Operation;
import org.apache.jena.fuseki.system.GraphLoadUtils;
import org.apache.jena.query.Dataset ;
import org.apache.jena.query.DatasetFactory ;
import org.apache.jena.query.Query ;
import org.apache.jena.rdf.model.Model ;
import org.apache.jena.rdf.model.ModelFactory ;
import org.apache.jena.riot.RiotException ;
import org.apache.jena.sparql.core.DatasetDescription ;
import org.apache.jena.sparql.core.DatasetGraph;
import org.apache.jena.sparql.core.DatasetGraphZero;

public class SPARQL_QueryGeneral extends SPARQL_Query {
    // In order to use as much of the main SPARQL code as possible, 
    // this freestanding servlet mimics being a dataset service, 
    // and inserts the loaded dataset at decideDataset.
    // ActionService understands this - the operation is null. 
    
    final static int MaxTriples = 100 * 1000 ;

    public SPARQL_QueryGeneral() {
        super() ;
    }

    @Override
    protected void validateRequest(HttpAction action) {}

    @Override
    protected void validateQuery(HttpAction action, Query query) {}

    @Override
    protected String mapRequestToDataset(HttpAction action) {
        return null ;
    }
    
    /** SPARQL_QueryGeneral is a servlet to be called directly. */
    @Override
    protected Operation chooseOperation(HttpAction action, DataService dataService) {
        return null;
    }

    @Override
    protected Pair<DatasetGraph, Query> decideDataset(HttpAction action, Query query, String queryStringLog) {
        DatasetDescription datasetDesc = getDatasetDescription(action, query);
        if ( datasetDesc == null ) {
            //ServletOps.errorBadRequest("No dataset description in protocol request or in the query string") ;
            return Pair.create(new DatasetGraphZero(), query);
        }
        
        // These will have been taken care of by the "getDatasetDescription"
        if ( query.hasDatasetDescription() ) {
            // Don't modify input.
            query = query.cloneQuery();
            query.getNamedGraphURIs().clear();
            query.getGraphURIs().clear();
        }
        return Pair.create(datasetFromDescriptionWeb(action, datasetDesc), query) ;
    }

    /**
     * Construct a Dataset based on a dataset description.
     * Loads graph from the web.
     */
    protected DatasetGraph datasetFromDescriptionWeb(HttpAction action, DatasetDescription datasetDesc) {
        try {
            if ( datasetDesc == null )
                return null ;
            if ( datasetDesc.isEmpty() )
                return null ;

            List<String> graphURLs = datasetDesc.getDefaultGraphURIs() ;
            List<String> namedGraphs = datasetDesc.getNamedGraphURIs() ;

            if ( graphURLs.size() == 0 && namedGraphs.size() == 0 )
                return null ;

            Dataset dataset = DatasetFactory.create() ;
            // Look in cache for loaded graphs!!

            // ---- Default graph
            {
                Model model = ModelFactory.createDefaultModel() ;
                for ( String uri : graphURLs ) {
                    if ( uri == null || uri.equals("") )
                        throw new InternalErrorException("Default graph URI is null or the empty string") ;

                    try {
                        GraphLoadUtils.loadModel(model, uri, MaxTriples) ;
                        action.log.info(format("[%d] Load (default graph) %s", action.id, uri)) ;
                    }
                    catch (RiotException ex) {
                        action.log.info(format("[%d] Parsing error loading %s: %s", action.id, uri, ex.getMessage())) ;
                        ServletOps.errorBadRequest("Failed to load URL (parse error) " + uri + " : " + ex.getMessage()) ;
                    }
                    catch (Exception ex) {
                        action.log.info(format("[%d] Failed to load (default) %s: %s", action.id, uri, ex.getMessage())) ;
                        ServletOps.errorBadRequest("Failed to load URL " + uri) ;
                    }
                }
                dataset.setDefaultModel(model) ;
            }
            // ---- Named graphs
            if ( namedGraphs != null ) {
                for ( String uri : namedGraphs ) {
                    if ( uri == null || uri.equals("") )
                        throw new InternalErrorException("Named graph URI is null or the empty string") ;

                    try {
                        Model model = ModelFactory.createDefaultModel() ;
                        GraphLoadUtils.loadModel(model, uri, MaxTriples) ;
                        action.log.info(format("[%d] Load (named graph) %s", action.id, uri)) ;
                        dataset.addNamedModel(uri, model) ;
                    }
                    catch (RiotException ex) {
                        action.log.info(format("[%d] Parsing error loading %s: %s", action.id, uri, ex.getMessage())) ;
                        ServletOps.errorBadRequest("Failed to load URL (parse error) " + uri + " : " + ex.getMessage()) ;
                    }
                    catch (Exception ex) {
                        action.log.info(format("[%d] Failed to load (named graph) %s: %s", action.id, uri, ex.getMessage())) ;
                        ServletOps.errorBadRequest("Failed to load URL " + uri) ;
                    }
                }
            }

            return dataset.asDatasetGraph() ;

        }
        catch (ActionErrorException ex) {
            throw ex ;
        }
        catch (Exception ex) {
            action.log.info(format("[%d] SPARQL parameter error: " + ex.getMessage(), action.id, ex)) ;
            ServletOps.errorBadRequest("Parameter error: " + ex.getMessage()) ;
            return null ;
        }
    }
}
